#include "KPF_Session.h"

KPF_Event::KPF_Event( void )
{
	memset( &m_EventParam, 0x00, sizeof(m_EventParam) );
	
	m_eEvent = EVENT_READ_ERROR;
}
	
KPF_Event::~KPF_Event( void )
{
	
}

void KPF_Event::setEventParam( CEventParam *pEventParam )
{
	if ( pEventParam )
		m_EventParam = *pEventParam;
}
	
void KPF_Event::setEvent( C_SESSION_EVENT eEvent )
{
	m_eEvent = eEvent;
}

C_SESSION_EVENT KPF_Event::getEvent( void )
{
	return m_eEvent;
}
	
CEventParam &KPF_Event::getEventParam( void )
{
	return m_EventParam;
}

////////////////////////////////////////////////
void KPF_Session::setEvent( C_SESSION_EVENT eEvent, CEventParam *pEventParam )
{
	m_Event.setEvent( eEvent );
	m_Event.setEventParam( pEventParam );
}
	
KPF_Event &KPF_Session::getEvent( void )
{
	return m_Event;
}

KPF_Session::KPF_Session( void )
	: pm_Session(NULL)
{
	
	
}

KPF_Session::~KPF_Session( void )
{
	if ( pm_Session )
	{
		destroy_session( pm_Session );
		pm_Session = NULL;
	}
}

void KPF_Session::setSocket( CSocket *pSocket )
{
	m_Socket.setSocket( pSocket );
}

KPF_Socket *KPF_Session::getSocket( void )
{
	return &m_Socket;
}
	
void KPF_Session::netAddr( const CNetAddr *pNetAddr )
{
	m_Addr.setAddr( pNetAddr );
}

KPF_Addr *KPF_Session::netAddr( void )
{
	return &m_Addr;
}

int32_t KPF_Session::onSessionBusiness( const struct CSession_t *pThis, 
					 		const CSocket *pSocket, 
				    		const int8u_t *pInData, 
				   	 	const int32_t iInDataLen, 
				   	 	const CNetAddr *pNetAddr, 
				   	 	void *pUserData )
{
	int32_t iRetCode = -1;
	
	KPF_Session *pSession = NULL;
	
	log_print( (int8_t *)"kpf session: onSessionBusiness------------->" );
	
	pSession = (KPF_Session *)pUserData;
	if ( pSession )
	{
		if ( pSocket )
			pSession->setSocket( (CSocket *)pSocket );
		
		if ( pNetAddr )
			pSession->netAddr( pNetAddr );
		
		iRetCode = pSession->handle_input( pSession->getSocket(  ), pInData, iInDataLen );
	}
	
	return iRetCode;
}

int32_t KPF_Session::onSessionEvent( const struct CSession_t *pThis, 
				 							const C_SESSION_EVENT eEvent, 
				 							const CEventParam *pEventParam, 
				 							const int32_t iEventParamSize, 
				 							void *pUserData )
{
	int32_t iRetCode = -1;
	KPF_Session *pSession = NULL;
	
	log_print( (int8_t *)"kpf session: onSessionEvent------------->" );
	
	pSession = (KPF_Session *)pUserData;
	if ( pSession )
	{
		pSession->setEvent( (C_SESSION_EVENT)eEvent, (CEventParam *)pEventParam );
		
		iRetCode = pSession->handle_event( pSession->getEvent(  ) );
	}
	
	return iRetCode;
}
